package com.xukai.springboot.datalayer.sys;

import com.xukai.springboot.entity.sys.t_sys_authority;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 目录管理
 */
@Mapper
public interface MenuMapper {

    /**
     * 获取目录列表
     */
    @SelectProvider(type = MenuMapperProvider.class, method = "GetMenuList")
    public List<t_sys_authority> GetMenuList(@Param("id") int parentID, @Param("state") int state);

    /**
     * 获取顶级菜单列表
     *
     * @return
     */
    @Select("select id ,`name` from t_sys_authority  where parentID=0 and aType=0 and state=1 order by sort")
    public List<t_sys_authority> GetTopMenu();

    /**
     * 获取用户可用目录列表
     */
    @SelectProvider(type = MenuMapperProvider.class, method = "GetMenuListByUserID")
    public List<t_sys_authority> GetMenuListByUserID(@Param("id") int parentID, @Param("userID") int userID);

    /**
     * 获取目录详情
     */
    @Select(" select id ,`name`,path,parentID,sort,state,createTime,aType from t_sys_authority  where id=#{id} ")
    public t_sys_authority GetMenu(@Param("id") int id);

    /**
     * 新建目录
     */
    @Insert(" INSERT INTO t_sys_authority (`name`,path,parentID,sort,state,createTime,aType )" +
            " values (#{name},#{path},#{parentId},#{sort},#{state},#{createTime},#{aType})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddMenu(t_sys_authority data);

    /**
     * 编辑目录
     */
    @Update(" update t_sys_authority  set `name`=#{name},path=#{path},parentID=#{parentID},sort=#{sort},state=#{state} where id=#{id}")
    public void UpdataMenu(@Param("id") int id, @Param("name") String name, @Param("sort") int sort, @Param("state") int state, @Param("path") String path
            , @Param("parentID") int parentID);

    /**
     * 修改目录状态
     */
    @Update(" update t_sys_authority  set state=#{state} where id=#{id}")
    public void UpdateState(@Param("id") int id, @Param("state") int state);

    /**
     * 判断目录名是否重复
     */
    @SelectProvider(type = MenuMapperProvider.class, method = "IsExist")
    public int IsExist(@Param("id") int id, @Param("name") String name);

    /**
     * 删除目录
     *
     * @param id
     */
    @Delete(" delete from t_sys_authority where id=#{id}")
    public void DelMenu(@Param("id") int id);

    /**
     * 目录管理辅助类
     */
    class MenuMapperProvider {

        /**
         * 获取目录列表
         *
         * @param parentID
         * @param state
         * @return
         */
        public String GetMenuList(int parentID, int state) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id ,`name`,path,parentID,sort,state,createTime,aType from t_sys_authority");
            sb.append(" where parentID=#{id} and aType=0 ");
            if (state >= 0) {
                sb.append(" and state=#{state}");
            }
            sb.append(" order by sort");
            return sb.toString();
        }

        /**
         * 获取用户可用列表
         *
         * @param parentID
         * @param userID
         * @return
         */
        public String GetMenuListByUserID(int parentID, int userID) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id ,`name`,path,parentID,sort,state,createTime,aType from t_sys_authority");
            sb.append(" where state=1 and parentID=#{id} and aType=0 ");
            sb.append(" order by sort");
            return sb.toString();
        }

        /**
         * 目录名是否存在
         *
         * @param id
         * @param Name
         * @return
         */
        public String IsExist(int id, String Name) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count(id) from t_sys_authority where name=#{name}");
            if (id > -1) {
                sb.append(" and id!=#{id}");
            }
            return sb.toString();
        }
    }

}
